using Entity;
using Microsoft.EntityFrameworkCore;

namespace API
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
            builder.Services.AddOpenApi();

            // ����api�ӿ�����Ĳ���
            builder.Services.AddCors(optios =>
                        {
                            optios.AddPolicy(
                                "AIYPolicy",
                                b => b
                                         .AllowAnyOrigin()// �����κ�������
                                         .AllowAnyMethod()// �����κη���������
                                         .AllowAnyHeader()// �����κ�����ͷ
                                );
                        });

            builder.Services.AddScoped<ApiDbContextFactory>();
            builder.Services.AddScoped(sp => sp.GetRequiredService<ApiDbContextFactory>().CreateDbContext(args));

            builder.Services.AddControllers().AddNewtonsoftJson(opts =>
            {
                /// api �ӿ�������� ��ת�� ��json��ʽʱ����ʹ��С�շ�����������ԭ�������
                opts.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                /// api �ӿ�������� ��ת�� ��json��ʽʱ����C#��DateTime���ڸ�ʽת��Ϊָ����ʽ��
                opts.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            });




            var app = builder.Build();

            app.UseCors("AIYPolicy");

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.MapOpenApi();
            }

            app.UseHttpsRedirection();

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}